package com.cy.核心编程.thead.theadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Single: 线性执行线程,线程创建也是线性的, 一个一个执行, 优点:就算有报错也可以执行完程序
 */
public class TestSingle {
    private static AtomicInteger i = new AtomicInteger();

    public static void main(String[] args) {
        ExecutorService es = Executors.newSingleThreadExecutor();

        for (int j = 0; j < 10; j++) {
            int key = TestSingle.i.getAndIncrement();
            System.out.println("创建子线程:" + key);
            es.execute(() -> {
                for (int i = 0; i < 100; i++) {
                    if (i==50){
                        int k = i/0;
                    }
                    System.out.println("子线程" +Thread.currentThread().getName()+"  key:"+ key + "===>>>>" + i);
                }
            });
        }

        for (int i = 0; i < 100; i++) {
            System.out.println("主线程" + i);
        }
    }
}
